<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
use app\admin\model\Rules as RuleModel;

class Rule extends Base
{
    //权限列表首页
    public function rules(){
        return view();
    }
    //权限列表数据请求接口
    public function rule_List()
    {
        $model = new RuleModel();
        $page = input('get.page');
        $limit = input('get.limit');
        $rule = $model->limit(($page - 1) * $limit, $limit)->select();
        $count = $model->count();
        return json(['code' => 0, 'msg' => "", 'count' => $count, 'data' => $rule]);
    }
    //权限添加页面渲染
    public function ruleAdd(){
        $rules = Db::name('auth_rule')->where('level',1)->select();
        $this->assign('rules',$rules);
        return view();
    }
    //权限添加数据处理
    public function ruleAddDo(){
        $data = input('post.');
        if($data["pid"] !== 0) {
            $level = Db::name('auth_rule')->field('level')->where('id', $data["pid"])->find();
            $data['level'] = $level['level']+1;
        }else{
            $data['level'] = 1;
        }
        if(isset($data['status'])){
            $data['status']=1;
        }else{
            $data['status']=0;
        }

        $model = new RuleModel();
        $res = $model->allowField(true)->save($data);
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '添加失败']);
        }
    }
    //权限表以及用户组中该权限的删除
    public function delRule(){
        $id = input('post.id');
        $group = Db::query("select `id`,`rules` from think_auth_group where FIND_IN_SET($id,rules)");
        if(!empty($group)) {
            foreach ($group as $v) {
                $rules = str_replace($id . ',', '', $v['rules']);
                $gid = $v['id'];
                Db::query("update `think_auth_group` set `rules` =" ." ' $rules ' "." where `id` = "."' $gid '");
            }
        }
        $model = new RuleModel();
        $res = $model->where('id',$id)->delete();
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2,'msg' => '删除失败']);
        }
    }
    //权限的编辑页面渲染
    public function Update(){
        $rules = Db::name('auth_rule')->where('level',1)->select();
        $id = input('get.id');

        $data = Db::name('auth_rule')->where('id',$id)->find();
        $this->assign('data',$data);
        $this->assign('rules',$rules);
        return view();
    }
    //权限编辑数据处理
    public function ruleUpdateDo(){
        $rule = input('post.');
        if($rule["pid"] !== 0) {
            $level = Db::name('auth_rule')->field('level')->where('id', $rule["pid"])->find();
            $rule['level'] = $level['level']+1;
        }else{
            $rule['level'] = 1;
        }

        if(isset($rule['status'])){
            $rule['status']=1;
        }else{
            $rule['status']=0;
        }
        $model = new RuleModel();
        $res = $model->isUpdate(true)->allowField(true)->save($rule);
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '修改失败']);
        }
    }
    //点击改变权限的状态数据处理
    public function ruleUpdateStatus(){
        $id = input('post.id');
        $data = RuleModel::get($id);
        if(input('post.status') == 'true'){
            $data->status = 1;
        }else{
            $data->status = 0;
        }
        $res = $data->allowField(true)->save();
        if($res){
            return json(['code' => 1, 'data' => $res, 'msg' => 'ok']);
        }else{
            return json(['code' => 2, 'msg' => '修改失败']);
        }
    }

}
